package com.example.hao.ximalaya.adapter.viewholder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.hao.ximalaya.R;
import com.example.hao.ximalaya.pojo.HotData;
import com.example.hao.ximalaya.pojo.Type2ListItem;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class Type2ViewHolder extends RecyclerView.ViewHolder {

    private Context context;


    @BindView(R.id.rv_item)
    RecyclerView rv_item;

    private MenuRVAdapter menuRVAdapter;

    public Type2ViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        ButterKnife.bind(this, itemView);
        initView();
    }

    private void initView() {
        rv_item.setLayoutManager(new GridLayoutManager(context, 5, LinearLayoutManager.VERTICAL, false));
        menuRVAdapter = new MenuRVAdapter(context);
        rv_item.setAdapter(menuRVAdapter);
    }

    /**
     * 为 ViewHolder 绑定数据
     *
     * @param data 数据对象
     */
    public void setData(HotData.ListBeanX data) {
        List<JsonObject> list = data.getList();
        ArrayList<Type2ListItem> items = new ArrayList<>();
        for (JsonObject obj : list) {
            Type2ListItem type2ListItem = new Gson().fromJson(obj, Type2ListItem.class);
            items.add(type2ListItem);
        }
        menuRVAdapter.setItems(items);
    }


    /**
     * 适配 菜单 的 RecyclerView的Adapter
     */
    public class MenuRVAdapter extends RecyclerView.Adapter<MenuRVAdapter.MenuViewHolder> {

        List<Type2ListItem> items;

        Context context;

        public MenuRVAdapter(Context context) {
            this.context = context;
        }

        public void setItems(List<Type2ListItem> items) {
            this.items = items;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View layout = LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_2_menu_item, parent, false);
//            //获取屏幕的宽度
//            int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
//
//            //获取Item布局的布局参数
//            ViewGroup.LayoutParams lp = layout.getLayoutParams();
//            //修改宽度为 屏幕的 1/5
//            lp.width = widthPixels / 5;
//            //重新设置Item的布局菜蔬
//            layout.setLayoutParams(lp);
            return new MenuViewHolder(layout);
        }

        @Override
        public void onBindViewHolder(@NonNull MenuViewHolder holder, int position) {
            Type2ListItem type2ListItem = items.get(position);

            String imageUrl = type2ListItem.getCoverPath();
            String title = type2ListItem.getTitle();

            holder.tvTitle.setText(title);
            Glide.with(context).load(imageUrl).into(holder.ivIcon);
        }


        @Override
        public int getItemCount() {
            return items == null ? 0 : items.size();
        }


        class MenuViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.iv_icon)
            ImageView ivIcon;
            @BindView(R.id.tv_title)
            TextView tvTitle;

            public MenuViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
